package com.cskaoyan.service.promotion;

import com.cskaoyan.bean.CommonData;
import com.cskaoyan.bean.bo.promotionbo.AdminBo;
import com.cskaoyan.bean.bo.promotionbo.MarketCouponBo;
import com.cskaoyan.bean.po.promotionpo.MarketCoupon;
import com.cskaoyan.bean.po.promotionpo.MarketCouponExample;
import com.cskaoyan.bean.po.promotionpo.MarketCouponUser;
import com.cskaoyan.bean.po.promotionpo.MarketCouponUserExample;
import com.cskaoyan.mapper.promotion.MarketCouponMapper;
import com.cskaoyan.mapper.promotion.MarketCouponUserMapper;

import com.cskaoyan.util.common.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * @author Zhang Tao
 * @version 1.0.0
 * @description
 * @date since 2022-07-16 21:35
 */
@Service
public class AdminCouponServiceImpl implements AdminCouponService {
    @Autowired
    MarketCouponMapper marketCouponMapper;
    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;

    @Override
    public CommonData<MarketCoupon> getCoupons(AdminBo adminBo) {
        PageHelper.startPage(adminBo.getPage(), adminBo.getLimit());

        MarketCouponExample example = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = example.createCriteria();
        if (adminBo.getName() != null) {
            criteria.andNameLike("%" + adminBo.getName() + "%");
        }
        if (adminBo.getType() != null) {
            criteria.andTypeEqualTo(adminBo.getType());
        }
        if (adminBo.getStatus() != null) {
            criteria.andStatusEqualTo(adminBo.getStatus());
        }
        criteria.andDeletedEqualTo(false);
        example.setOrderByClause(adminBo.getSort() + " " + adminBo.getOrder());
        List<MarketCoupon> adVoList = marketCouponMapper.selectByExample(example);
        PageInfo<MarketCoupon> adminAdVoPageInfo = new PageInfo<>(adVoList);
        return CommonData.data(adminAdVoPageInfo);

    }

    @Transactional
    @Override
    public MarketCoupon createAdminCoupon(MarketCouponBo marketCouponBo) {
        MarketCoupon marketCoupon = new MarketCoupon();
        marketCoupon.setGoodsType(marketCouponBo.getGoodsType());
        marketCoupon.setTotal(marketCouponBo.getTotal());
        marketCoupon.setMin(marketCouponBo.getMin());
        marketCoupon.setName(marketCouponBo.getName());
        marketCoupon.setLimit(marketCouponBo.getLimit());
        marketCoupon.setDays(marketCouponBo.getDays());
        marketCoupon.setTag(marketCouponBo.getTag());
        marketCoupon.setDesc(marketCouponBo.getDesc());
        marketCoupon.setStatus(marketCouponBo.getStatus());
        marketCoupon.setTimeType(marketCouponBo.getTimeType());
        marketCoupon.setDeleted(false);
        marketCoupon.setAddTime(DateUtils.time());
        marketCoupon.setUpdateTime(DateUtils.time());
        // if(marketCouponBo.getType()==2){
        //     String s="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        //     char[] chars = s.toCharArray();
        //     StringBuilder s1 = new StringBuilder();
        //     for (int i = 0; i < 8; i++) {
        //         Random random = new Random();
        //         int index = random.nextInt(s.length());
        //         s1.append(chars[index]);
        //     }
        //
        // }
        String s = UUID.randomUUID().toString();
        String s1 = s.substring(0, 8);
        marketCoupon.setCode(s1);
        marketCouponMapper.insertSelective(marketCoupon);
        Integer id = marketCoupon.getId();
        MarketCoupon marketCoupon1 = marketCouponMapper.selectByPrimaryKey(id);
        return marketCoupon1;

    }

    @Transactional
    @Override
    public MarketCoupon updateAdminCoupon(MarketCoupon marketCoupon) {
        marketCoupon.setUpdateTime(DateUtils.time());
        // if(marketCoupon.getType()==2){
        //     String s="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        //     char[] chars = s.toCharArray();
        //     StringBuilder s1 = new StringBuilder();
        //     for (int i = 0; i < 8; i++) {
        //         Random random = new Random();
        //         int index = random.nextInt(s.length());
        //         s1.append(chars[index]);
        //     }}
        String s = UUID.randomUUID().toString();
        String s1 = s.substring(0, 8);
            marketCoupon.setCode(s1);


        marketCouponMapper.updateByPrimaryKeySelective(marketCoupon);

        return marketCoupon;
    }

    @Transactional
    @Override
    public void deleteAdminCoupons(MarketCoupon marketCoupon) {
        marketCoupon.setDeleted(true);
        marketCouponMapper.updateByPrimaryKeySelective(marketCoupon);
    }

    @Override
    public MarketCoupon readAdminCoupons(Integer id) {
        MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(id);
        return marketCoupon;

    }

    @Override
    public CommonData<MarketCouponUser> getCouponListUser(AdminBo adminBo) {
        PageHelper.startPage(adminBo.getPage(), adminBo.getLimit());
        MarketCouponUserExample example = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = example.createCriteria();
        if (adminBo.getUserId() != null) {
            criteria.andUserIdEqualTo(adminBo.getUserId());
        }
        if (adminBo.getStatus() != null) {
            criteria.andStatusEqualTo(adminBo.getStatus());
        }
        example.setOrderByClause(adminBo.getSort() + " " + adminBo.getOrder());
        List<MarketCouponUser> marketCouponUsers = marketCouponUserMapper.selectByExample(example);
        PageInfo<MarketCouponUser> marketCouponUserPageInfo = new PageInfo<>(marketCouponUsers);
        return CommonData.data(marketCouponUserPageInfo);
    }


}
